<?php

class AudioController extends Controller {
	
	public $defaultAction = 'view';
	
	/**
	 * 显示音频
	 *
	 * @param $id int       	
	 */
	public function actionView($id) {
		
		$audio_model = Audios::model ()->getAudio ( $id );
		$like_model = AudioLikes::model ();
		$uid = Yii::app ()->user->id;
		
		if ($uid) {
			$like_model = AudioLikes::model ()->findByAttributes ( array ('like_from' => $uid, 'audio_id' => $id ) );
		} else {
		
		}
		
		if ($audio_model) {

			$this->_setHistory ( $audio_model );
			
			$playList = array(
					'title'=>$audio_model -> audio_name,
					'artist'=>$audio_model-> audio_from,
					'album'=>$audio_model-> audio_from,
					'cover'=>Yii::app() -> params -> imgAccessPath.$audio_model-> audio_cover,
					'mp3'=>Yii::app() -> params -> audioAccessPath.$audio_model-> audio_file,
					'ogg'=>Yii::app() -> params -> audioAccessPath.$audio_model-> audio_file);
			
			$mp3file = '['.CJSON::encode($playList).']';
			
			$this->render ( 'view', array ('audio_model' => $audio_model, 'like_model' => $like_model ,'mp3file'=>$mp3file) );
		} else {
			$this->render ( 'notfound' );
		}
	
	}
	
	/**
	 * 赞音频
	 *
	 * @param $audio id       	
	 */
	public function actionLike() {
		
		$uid = Yii::app ()->user->id;
		$audio_id = Yii::app ()->request->getParam ( 'audio_id' );
		
		//音频 model
		$audio_mdoel = Audios::model() -> findByAttributes(array('audio_saltid'=>$audio_id));
		
		//赞 model
		$audio_like = AudioLikes::model ()->findByAttributes ( array ('audio_id' => $audio_mdoel ->audio_id , 'like_from' => $uid ) );
		
		if (! $audio_like) {
			$this->_addLike ( $audio_mdoel );
			echo json_encode ( array ('res' => '200' ) );
		} else {
			$this->_removeLike ( $audio_mdoel );
			echo json_encode ( array ('res' => '200' ) );
		}
	
	}
	
	/**
	 * 取消赞
	 * @param $audio_info unknown_type       	
	 */
	private function _removeLike($audio_info) {
		
		$uid = Yii::app ()->user->id;
		$audio_like_model = new AudioLikes ();
		$cdbCriteria = new CDbCriteria ();
		$cdbCriteria->compare ( 'audio_id', $audio_info->audio_id );
		$cdbCriteria->compare ( 'like_from', $uid );
		$cdbCriteria->limit = 1;
		$audio_like_model->deleteAll ( $cdbCriteria );
		
		$audio_info->updateCounters ( array ('audio_likes' => '-1' ), 'audio_id=' . $audio_info->audio_id );
	}
	
	/**
	 * 添加赞
	 * @param $audio_info unknown_type       	
	 */
	private function _addLike($audio_info) {
		
		$uid = Yii::app ()->user->id;
		$audio_like_model = new AudioLikes ();
		$audio_like_model->like_id = NULL;
		$audio_like_model->like_from = $uid;
		$audio_like_model->like_to = $audio_info->audio_from;
		$audio_like_model->audio_id = $audio_info->audio_id;
		$audio_like_model->dateline = time ();
		$audio_like_model->save ();
		$audio_info->updateCounters ( array ('audio_likes' => '1' ), 'audio_id=' . $audio_info->audio_id );
	
	}
	
	/**
	 * 记录访问的记录
	 * @param $audio_model unknown_type       	
	 */
	private function _setHistory($audio_model) {
		
		$uid = Yii::app ()->user->id;
		
		$audio_history = AudioHistory::model ()->findByAttributes ( array ('audio_id' => $audio_model->audio_id ) );
		
		if ($audio_history) {
			$audio_history->dateline = time ();
			$audio_history->update ();
			return $audio_history;
		} else {
			$audio_history = new AudioHistory ();
			$audio_history->history_id = NULL;
			$audio_history->history_from = $uid;
			$audio_history->audio_id = $audio_model->audio_id;
			$audio_history->dateline = time ();
			$audio_history->starttime = 0;
			$audio_history->save ();
			return $audio_history;
		}
	
	}

}